const db = require('../config/database');

class Location {
  constructor(
    id = null,
    locationCode,
    aisle,
    row,
    column,
    level,
    status = 'enabled'
  ) {
    this.id = id;
    this.locationCode = locationCode;
    this.aisle = aisle;
    this.row = row;
    this.column = column;
    this.level = level;
    this.status = status; // enabled or disabled
  }

  // 保存货位
  save(callback) {
    const sql = `
      INSERT INTO locations (
        location_code, aisle, row, column, level, status
      ) VALUES (?, ?, ?, ?, ?, ?)
    `;

    const params = [
      this.locationCode, this.aisle, this.row, this.column, this.level, this.status
    ];

    db.run(sql, params, function(err) {
      if (err) {
        return callback(err);
      }
      callback(null, { id: this.lastID });
    });
  }

  // 获取所有货位
  static findAll(callback) {
    const sql = 'SELECT * FROM locations ORDER BY location_code';
    db.all(sql, [], (err, rows) => {
      if (err) {
        return callback(err);
      }
      callback(null, rows);
    });
  }

  // 根据状态获取货位
  static findByStatus(status, callback) {
    const sql = 'SELECT * FROM locations WHERE status = ? ORDER BY location_code';
    db.all(sql, [status], (err, rows) => {
      if (err) {
        return callback(err);
      }
      callback(null, rows);
    });
  }

  // 根据货位编码获取货位
  static findByLocationCode(locationCode, callback) {
    const sql = 'SELECT * FROM locations WHERE location_code = ?';
    db.get(sql, [locationCode], (err, row) => {
      if (err) {
        return callback(err);
      }
      callback(null, row);
    });
  }

  // 根据ID获取货位
  static findById(id, callback) {
    const sql = 'SELECT * FROM locations WHERE id = ?';
    db.get(sql, [id], (err, row) => {
      if (err) {
        return callback(err);
      }
      callback(null, row);
    });
  }

  // 更新货位状态
  static updateStatus(id, status, callback) {
    const sql = 'UPDATE locations SET status = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?';
    db.run(sql, [status, id], function(err) {
      if (err) {
        return callback(err);
      }
      callback(null, { updated: this.changes > 0 });
    });
  }

  // 批量更新货位状态
  static updateStatusBatch(ids, status, callback) {
    if (!Array.isArray(ids) || ids.length === 0) {
      return callback(null, { updated: 0 });
    }
    
    // 构建占位符
    const placeholders = ids.map(() => '?').join(',');
    const sql = `UPDATE locations SET status = ?, updated_at = CURRENT_TIMESTAMP WHERE id IN (${placeholders})`;
    
    // 参数数组：状态 + 所有ID
    const params = [status, ...ids];
    
    db.run(sql, params, function(err) {
      if (err) {
        return callback(err);
      }
      callback(null, { updated: this.changes });
    });
  }

  // 根据查询条件获取货位
  static findByQuery(queryParams, callback) {
    let sql = 'SELECT * FROM locations WHERE 1=1';
    const params = [];
    
    // 货位编码查询
    if (queryParams.locationCode) {
      sql += ' AND location_code LIKE ?';
      params.push(`%${queryParams.locationCode}%`);
    }
    
    // 巷道查询
    if (queryParams.aisle) {
      sql += ' AND aisle = ?';
      params.push(queryParams.aisle);
    }
    
    // 排查询
    if (queryParams.row) {
      sql += ' AND row = ?';
      params.push(queryParams.row);
    }
    
    // 列查询
    if (queryParams.column) {
      sql += ' AND column = ?';
      params.push(queryParams.column);
    }
    
    // 层查询
    if (queryParams.level) {
      sql += ' AND level = ?';
      params.push(queryParams.level);
    }
    
    // 状态查询
    if (queryParams.status) {
      sql += ' AND status = ?';
      params.push(queryParams.status);
    }
    
    sql += ' ORDER BY location_code';
    
    db.all(sql, params, (err, rows) => {
      if (err) {
        return callback(err);
      }
      callback(null, rows);
    });
  }

  // 删除货位
  static delete(id, callback) {
    const sql = 'DELETE FROM locations WHERE id = ?';
    db.run(sql, [id], function(err) {
      if (err) {
        return callback(err);
      }
      callback(null, { deleted: this.changes > 0 });
    });
  }
}

module.exports = Location;